﻿
using Sunny.UI.Win32;
using Sunny.UI;
using System.Net;


namespace WinApp.Until
{
    public class FileHelper
    {
        /// <summary>
        /// 根据网络地址获取图片
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns></returns>
        public static Image? LoadImageFromWebUriAsync(string imageUrl)
        {
            try
            {
                Image result = null;

                if (string.IsNullOrEmpty(imageUrl))
                {
                    return null;
                    //throw new ArgumentException("ImageUrl cannot be null or empty.");
                }

                using (var webC = new WebClient())
                {
                    using (var streamFilePic = webC.OpenRead(imageUrl))
                    {
                        result = Image.FromStream(streamFilePic);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return null;
            }

        }

        public static Image? loadImageFromLocalPath(string path)
        {
            try
            {

                // 检查文件是否存在
                if (!File.Exists(path))
                {
                    //throw new FileNotFoundException($"无法找到图片文件：{path}");
                    return null;
                }

                //string imagePath = @"C:\Users\YourUsername\Pictures\image.jpg"; // 替换为你的图片路径
                Image image = Image.FromFile(path);
                return image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static Bitmap? loadBitmapFromLocalPath(string path, int width = 200, int height = 200)
        {
            try
            {
                using (Image originalImage = Image.FromFile(path))
                {

                    Bitmap resultImage = new Bitmap(width, height);
                    using (Graphics graphics = Graphics.FromImage(resultImage))
                    {
                        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                        graphics.DrawImage(originalImage, 0, 0, width, height);
                    }

                    // 此时，resultImage 是你最终需要的图像
                    return resultImage;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static async Task<Stream> GetPdfStreamAsync(string pdfUrl)
        {
            // 使用 HttpClient 获取数据
            using (HttpClient client = new HttpClient())
            {
                // 发送请求，获取响应
                HttpResponseMessage response = await client.GetAsync(pdfUrl);

                // 检查响应是否成功
                if (response.IsSuccessStatusCode)
                {
                    // 获取响应中的内容作为流
                    Stream responseStream = await response.Content.ReadAsStreamAsync();

                    // 创建一个 MemoryStream 来存储数据
                    MemoryStream memoryStream = new MemoryStream();

                    // 将响应流复制到 MemoryStream，以便持久化和随时访问
                    await responseStream.CopyToAsync(memoryStream);

                    // 重置 MemoryStream 的位置到开始，以便读取
                    memoryStream.Seek(0, SeekOrigin.Begin);

                    // 返回 MemoryStream
                    return memoryStream;
                }
                else
                {
                    // 如果响应不成功，抛出异常或者返回 null
                    //throw new InvalidOperationException("无法获取 PDF 流");
                    UIMessageDialog.ShowMessageDialog("Not get pdf stream", UILocalize.WarningTitle, showCancelButton: false, UIStyle.Orange, false);
                    return null;
                }
            }
        }

        public static long GetWebFileSize(string path)
        {
            // 创建一个 HttpWebRequest 实例
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
            request.Method = "HEAD";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                // 从响应头中获取资源的大小
                long resourceSize = response.ContentLength;
                return resourceSize;
            }
        }

        /// <summary>
        /// 获取文件后缀
        /// </summary>
        public static string GetFileSuffix(string fileName)
        {
            // 文件路径
            //string filePath = @"C:\Path\To\Your\File.txt";

            // 获取文件的扩展名
            string extension = Path.GetExtension(fileName);

            // 输出扩展名
            //Console.WriteLine("文件扩展名是: " + extension);
            return extension;
        }

    }
}
